package edu.pku.sei.modeler.uml.commands.compositestructure;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.Collaborations.Collaboration;
import edu.pku.sei.modeler.model.uml.Interfaces.Interface;
import edu.pku.sei.modeler.model.uml.Kernel.Classifier;
import edu.pku.sei.modeler.model.uml.Ports.Port;
import edu.pku.sei.modeler.model.uml.StructuredClasses.StructuredClass;
import edu.pku.sei.modeler.uml.commands.UMLElementCreateCommandFactory;
import edu.pku.sei.modeler.uml.commands.component.PortCreateCommand;

public class CompositeStructureDiagramElementCreateCommandFactory extends UMLElementCreateCommandFactory{
	public static final CompositeStructureDiagramElementCreateCommandFactory INSTANCE = new CompositeStructureDiagramElementCreateCommandFactory();
	private CompositeStructureDiagramElementCreateCommandFactory(){
		
	}
	@Override
	public Command createCommand(MetaNode node, MetaDiagram diagram,
			Rectangle constraint) {
		if (node.getModelElement() instanceof Port) {
			return null;
		} else if (node.getModelElement() instanceof Interface
				&& (((Interface) node.getModelElement()).getType() == Interface.TYPE_USEINTERFACE || ((Interface) node
						.getModelElement()).getType() == Interface.TYPE_IMPLEMENTINTERFACE)) {
			return null;
		} else {
			return super.createCommand(node, diagram, constraint);
		}
	}

	@Override
	public Command createCommand(MetaNode node, MetaNode fatherNode,
			Rectangle constraint) {
		MetaModelElement childModel = node.getModelElement();
		MetaModelElement fatherModel = fatherNode.getModelElement();
		 if (childModel instanceof Port && fatherModel instanceof StructuredClass) {
			return new PortCreateCommand(node, fatherNode, constraint);
		} else if (childModel instanceof StructuredClass && fatherModel instanceof StructuredClass) { 
			return new NestedStructuredClassCreateCommand(node, fatherNode, constraint);
		} else if (fatherModel instanceof Collaboration && childModel instanceof Classifier) {
			return new ElementNestedInCollaborationCreateCommand(node, fatherNode, constraint);
		}
		else
			return null;
	}
}
